package com.wn.budget.service;

import com.wn.budget.model.Project;
import com.wn.budget.model.ProjectMaterial;
import com.wn.budget.repository.ProjectMaterialRepository;
import com.wn.budget.repository.ProjectRepository;
//import com.wn.budget.vo.ProjectData;
import com.wn.budget.vo.ProjectData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProjectService {
    @Autowired
    private ProjectRepository projectRepository;

    @Autowired
    private ProjectMaterialRepository projectMaterialRepository;

    public Page<Project> findAll(int pageIndex, int pageSize) {
        Page<Project> list = projectRepository.findAll(PageRequest.of(pageIndex, pageSize));
        return list;
    }

    /**
     * 获取导出excel所需数据
     * <p>
     * //     * @param projectId
     */
//    public ProjectData findProjectData(String projectId) {
//        Project project = projectRepository.findById(projectId).get();
//        List<ProjectMaterial> projectMaterials = projectMaterialRepository.findByProjectId(projectId);
//        ProjectData projectData = new ProjectData();
//        projectData.setProject(project);
//        projectData.setProjectMaterials(projectMaterials);
//        return projectData;
//    }
    public Page<Project> findProjects(String userId, int pageIndex, int pageSize, Date beginDate, Date endDate, String customerName) {

//        Project project = new Project();
//        project.setAdminId(userId);
//        project.setCustomerName(customerName);
//        Example example = Example.of(project);
//        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
////        Page<Project> page= projectRepository.findAll(example,PageRequest.of(pageIndex, pageSize));
        Page<Project> page=  projectRepository.findProjects(customerName,beginDate,endDate,userId,PageRequest.of(pageIndex, pageSize));
        return page;
    }

    /**
     * 获取导出excel所需数据
     *
     * @param projectId
     */
    public ProjectData findProjectData(String projectId) {
        Project project = projectRepository.findById(projectId).get();
        List<ProjectMaterial> projectMaterials = projectMaterialRepository.findByProjectId(projectId);
        ProjectData projectData=new ProjectData();
        projectData.setProject(project);
        projectData.setProjectMaterials(projectMaterials);
        return projectData;
    }

}
